#include "ccd290_extract.h"

void swap_endian16(uint16_t *a){
	uint16_t tmp = *a;
	uint16_t res;
	res = ((tmp & 0x00ff) << 4 ) | ((tmp & 0xff00 ) >> 4);

    *a = res;
/*	return res;*/
}

void swap_endian32(uint32_t *a){
	uint32_t tmp = *a;
	uint32_t res;
	res = ((tmp & 0x000000ff) << 24 )
		| ((tmp & 0x0000ff00) << 8)
		| ((tmp & 0x00ff0000) >> 8)
		| ((tmp & 0xff000000 ) >> 24);

    *a = res;
/*	return res;*/
}

int16_t fun2s( uint16_t d ) {
    int16_t res;

    if( getbit(d, 15) ) {
        res = -(~d + 1);        // this is the correct one
        // res = (int16_t)(~d+1);
    }
    else {
        res = d;
    }

    // res = d;
    return res;
}


//  find given postfix in a string
int find_postfix(   char const* str,                                                                                                                                                                                                                       
                    char const* postfix ){
    printf("debug str = %s\n", str);

    int find = 0;

    int len_pfx = strlen(postfix);
    int len_str = strlen(str);

    char *tmp = NULL;
    tmp = (char*)str + (len_str-len_pfx);

    find = strcmp(tmp, postfix) == 0;

    return find;
}


//  used to replace postfix of a given filename
void replace_postfix(   char const*str, 
                        char const *pfix_old, 
                        char const *pfix_new, 
                        char new_str[] ){

    char sub_str[1024];
    
    int pos = strstr(str, pfix_old) - str;

    int i;    
    for(i=0; i<pos; i++){
        sub_str[i] = str[i];
    }
    
    sprintf(new_str, "%s%s", sub_str, pfix_new);
}
